package com.example.zhidao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhidao.dao.CgoodsMapper;
import com.example.zhidao.dao.CommentsMapper;
import com.example.zhidao.model.po.Cgoods;
import com.example.zhidao.service.CgoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

@Service
public class CgoodsServiceImpl extends ServiceImpl<CgoodsMapper, Cgoods> implements CgoodsService {

    @Resource
    CgoodsMapper cgoodsMapper;

    @Resource
    CommentsMapper commentsMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addcgood(Cgoods cgoods) {
        try{
            cgoodsMapper.insert(cgoods);
            commentsMapper.addcoomgood(cgoods.getCid());
        }catch (Exception e){
            // 回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delcgood(Cgoods cgoods) {
        try{
            cgoodsMapper.deleteById(cgoods);
            commentsMapper.delcoomgood(cgoods.getCid());
        }catch (Exception e){
            // 回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
}
